#include "menu.h"
#include "oled.h"
#include "delay.h"
#include "key.h"
#include "ui.h"
#include "led.h"
#include "control.h"

// UI����
/****************************************************/

Menu_table table[10] =
{
	{(*Home), 0, 0, 0, 1, 0}, // 主页

	{(*Music), 4, 1, 2, 5, 0}, 		// 菜单音乐页面
	{(*Palygame), 1, 2, 3, 6, 0},	// 菜单游戏页面
	{(*Setting), 2, 3, 4, 7, 0},	// 菜单设置页面
	{(*Info), 3, 4, 1, 8, 0},		// 菜单信息页面

	{(*PlayMusic), 5, 5, 5, 5, 1}, 		// 播放音乐页面
	{(*ConrtolGame), 6, 6, 6, 6, 2},	// 游戏控制页面
	{(*Set), 7, 7, 7, 9, 3},			// LED设置页面
	{(*Information), 8, 8, 8, 8, 4},	// 详细信息页面

	{(*LED), 9, 9, 9, 7, 3}, // LED控制
};

static uint8_t func_index = 0;	   // 当前页面的编号
void (*current_operation_index)(); // 菜单页面运行的函数
static short index_flag = 0;	   // 标志页面切换方向
static short ui_flag = 1;		   // 用于标志是否需要刷新UI组件

void Menu_key_set(uint8_t val)
{
	if ((val == 1) && (func_index != 6))
	{
		func_index = table[func_index].previous; // 点击previous指向上一项
		if (func_index == 1 || func_index == 2 || func_index == 3 || func_index == 4)
			index_flag = 2;
	}

	if ((val == 4) && (func_index != 6))
	{
		func_index = table[func_index].next; // 点击next指向下一项
		if (func_index == 1 || func_index == 2 || func_index == 3 || func_index == 4)
			index_flag = 1;
	}

	if ((val == 2) && (func_index != 6))
	{
		func_index = table[func_index].enter; // 点击enter指向确定项
		OLED_Clear();
		ui_flag = 1;
	}

	if (val == 3)
	{
		func_index = table[func_index].back; // 点击back指向返回项
		OLED_Clear();
		ui_flag = 1;
	}

	current_operation_index = table[func_index].current_operation; //指向当前页面的函数
	(*current_operation_index)();								   //执行当前页面的函数
}

// 0
void Home()
{
	RTC_display();
	OLED_DrawBMP(40, 2, 88, 8, gImage_home);
	if (ui_flag)
	{
		// UI显示
		OLED_DrawBMP(0, 0, 16, 2, signal_BMP);
		OLED_DrawBMP(16, 0, 32, 2, gImage_bulethouch);
		OLED_DrawBMP(112, 0, 128, 2, gImage_engery);
		OLED_DrawBMP(16, 6, 32, 8, gImage_yes);
		OLED_DrawBMP(16, 4, 24, 5, gImage_left);
		OLED_DrawBMP(103, 4, 111, 5, gImage_right);
		OLED_DrawBMP(95, 6, 111, 8, gImage_back);
		ui_flag = 0;
	}
}

// 1
void Music()
{
	RTC_display();
	if (ui_flag)
	{
		// UI显示
		OLED_DrawBMP(0, 0, 16, 2, signal_BMP);
		OLED_DrawBMP(16, 0, 32, 2, gImage_bulethouch);
		OLED_DrawBMP(112, 0, 128, 2, gImage_engery);
		OLED_DrawBMP(16, 6, 32, 8, gImage_yes);
		OLED_DrawBMP(16, 4, 24, 5, gImage_left);
		OLED_DrawBMP(103, 4, 111, 5, gImage_right);
		OLED_DrawBMP(95, 6, 111, 8, gImage_back);
		ui_flag = 0;
	}
	if (index_flag)
	{
		if (index_flag == 1) //向下一项切换UI滑动效果
		{
			OLED_RightDrawBMP(40, 2, 88, 8, gImage_info, gImage_music);
			index_flag = 0;
		}
		else if (index_flag == 2) //向上一项切换UI滑动效果
		{
			OLED_LeftDrawBMP(40, 2, 88, 8, gImage_playgame, gImage_music);
			index_flag = 0;
		}
	}

	OLED_DrawBMP(40, 2, 88, 8, gImage_music);
}

// 2
void Palygame()
{
	RTC_display();
	if (ui_flag)
	{
		OLED_DrawBMP(0, 0, 16, 2, signal_BMP);
		OLED_DrawBMP(16, 0, 32, 2, gImage_bulethouch);
		OLED_DrawBMP(112, 0, 128, 2, gImage_engery);
		OLED_DrawBMP(16, 6, 32, 8, gImage_yes);
		OLED_DrawBMP(16, 4, 24, 5, gImage_left);
		OLED_DrawBMP(103, 4, 111, 5, gImage_right);
		OLED_DrawBMP(95, 6, 111, 8, gImage_back);
		ui_flag = 0;
	}
	if (index_flag)
	{
		if (index_flag == 1)
		{
			OLED_RightDrawBMP(40, 2, 88, 8, gImage_music, gImage_playgame);
			index_flag = 0;
		}
		else if (index_flag == 2)
		{
			OLED_LeftDrawBMP(40, 2, 88, 8, gImage_setting, gImage_playgame);
			index_flag = 0;
		}
	}
	OLED_DrawBMP(40, 2, 88, 8, gImage_playgame);
}

// 3
void Setting()
{
	RTC_display();
	if (ui_flag)
	{
		OLED_DrawBMP(0, 0, 16, 2, signal_BMP);
		OLED_DrawBMP(16, 0, 32, 2, gImage_bulethouch);
		OLED_DrawBMP(112, 0, 128, 2, gImage_engery);
		OLED_DrawBMP(16, 6, 32, 8, gImage_yes);
		OLED_DrawBMP(16, 4, 24, 5, gImage_left);
		OLED_DrawBMP(103, 4, 111, 5, gImage_right);
		OLED_DrawBMP(95, 6, 111, 8, gImage_back);
		ui_flag = 0;
	}
	if (index_flag)
	{
		if (index_flag == 1)
		{
			OLED_RightDrawBMP(40, 2, 88, 8, gImage_playgame, gImage_setting);
			index_flag = 0;
		}
		else if (index_flag == 2)
		{
			OLED_LeftDrawBMP(40, 2, 88, 8, gImage_info, gImage_setting);
			index_flag = 0;
		}
	}
	OLED_DrawBMP(40, 2, 88, 8, gImage_setting);
}

// 4
void Info()
{
	RTC_display();
	if (ui_flag)
	{
		OLED_DrawBMP(0, 0, 16, 2, signal_BMP);
		OLED_DrawBMP(16, 0, 32, 2, gImage_bulethouch);
		OLED_DrawBMP(112, 0, 128, 2, gImage_engery);
		OLED_DrawBMP(16, 6, 32, 8, gImage_yes);
		OLED_DrawBMP(16, 4, 24, 5, gImage_left);
		OLED_DrawBMP(103, 4, 111, 5, gImage_right);
		OLED_DrawBMP(95, 6, 111, 8, gImage_back);
		ui_flag = 0;
	}
	if (index_flag)
	{
		if (index_flag == 1)
		{
			OLED_RightDrawBMP(40, 2, 88, 8, gImage_setting, gImage_info);
			index_flag = 0;
		}
		else if (index_flag == 2)
		{
			OLED_LeftDrawBMP(40, 2, 88, 8, gImage_music, gImage_info);
			index_flag = 0;
		}
	}
	OLED_DrawBMP(40, 2, 88, 8, gImage_info);
}

// 5
void PlayMusic()
{
	play_music(0);
	func_index = table[func_index].back;
	OLED_Clear();
}
// 6
void ConrtolGame()
{
	Game_control();
	func_index = table[func_index].back; // ����back���º��������
	OLED_Clear();
}

// 7
void Set()
{
	OLED_ShowString(0, 0, "Peripherals:Lights", 16);
	OLED_ShowString(0, 4, "Status:Closed", 16);
	LED1(0);
}

// 8
void Information()
{
	OLED_ShowString(0, 0, "Author:", 16);
	OLED_ShowCHinese(56, 0, 3);
	OLED_ShowCHinese(72, 0, 4);
	OLED_ShowCHinese(88, 0, 5);
	OLED_ShowString(0, 2, "Date:2023/7/25", 16);
	OLED_ShowString(0, 4, "Lab: Multi-level menu", 16);
}

// 9
void LED()
{
	OLED_ShowString(0, 0, "Peripherals:Lights", 16);
	OLED_ShowString(0, 4, "Status: Open", 16);
	LED1(1);
}

void RTC_display()
{

	/* Display date Format : yy/mm/dd */

	/* Display time Format : hh:mm:ss */
	/***********
	OLED_ShowNum(40,0,GetTime.Hours,2,16);				//hour
	OLED_ShowString(57,0,":",16);
	OLED_ShowNum(66,0,GetTime.Minutes,2,16);			//min
	OLED_ShowString(83,0,":",16);
	OLED_ShowNum(93,0,GetTime.Seconds,2,16);			//seconds
	**********/

	OLED_ShowNum(36, 0, 11, 2, 16); // hour
	OLED_ShowString(52, 0, ":", 16);
	OLED_ShowNum(60, 0, 11, 2, 16); // min
	OLED_ShowString(76, 0, ":", 16);
	OLED_ShowNum(84, 0, 11, 2, 16); // seconds
}